package top.ajun.mall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.ajun.common.utils.PageUtils;
import top.ajun.common.utils.Query;
import top.ajun.mall.ware.dao.PurchaseDetailDao;
import top.ajun.mall.ware.entity.PurchaseDetailEntity;
import top.ajun.mall.ware.service.PurchaseDetailService;

import java.util.List;
import java.util.Map;

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
            new Query<PurchaseDetailEntity>().getPage(params),
            Wrappers.query()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = Wrappers.lambdaQuery();

        // 检索关键字
        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)) {
            wrapper.and(w -> w
                .eq(PurchaseDetailEntity::getPurchaseId, key).or()
                .eq(PurchaseDetailEntity::getSkuId, key).or()
                .eq(PurchaseDetailEntity::getId, key)
            );
        }

        // 状态
        String status = (String) params.get("status");
        if (StrUtil.isNotEmpty(status)) {
            wrapper.eq(PurchaseDetailEntity::getStatus, status);
        }

        // 仓库 id
        String wareId = (String) params.get("wareId");
        if (StrUtil.isNotEmpty(wareId)) {
            wrapper.eq(PurchaseDetailEntity::getWareId, wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        return this.list(Wrappers.<PurchaseDetailEntity>lambdaQuery().eq(PurchaseDetailEntity::getPurchaseId, id));
    }

}